package stark.app.function.netusage;

import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.netusage.lib.AppLimitService;
import com.stark.netusage.lib.AppNetUsageInfo;
import com.stark.netusage.lib.NetUsageInfoList;
import com.stark.netusage.lib.NetUsageUtil;

import java.util.List;

import stark.app.R;
import stark.app.databinding.FragmentNetUsageTodayDemoBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.PermissionUtil;

/**
 * Author：daizhongbin on 2022/8/10
 * Email：1027154338@qq.com
 * Desc:
 */
public class NetUsageTodayFragment extends BaseNoModelFragment<FragmentNetUsageTodayDemoBinding> {

    private NetUsageAppAdapter mUsageAppAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_net_usage_today_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.llMobile.setOnClickListener(this);
        mDataBinding.llWifi.setOnClickListener(this);
        mDataBinding.tvMobile.setOnClickListener(this);
        mDataBinding.tvWifi.setOnClickListener(this);

        //rv app
        mDataBinding.rvApp.setLayoutManager(new LinearLayoutManager(getContext()));
        NetUsageAppAdapter usageAppAdapter = new NetUsageAppAdapter();
        usageAppAdapter.setOnItemClickListener(this);
        mUsageAppAdapter = usageAppAdapter;
        mDataBinding.rvApp.setAdapter(usageAppAdapter);
    }

    @Override
    protected void initData() {
        if (PermissionUtil.hasUsageStatsPermission()) {
            updateData();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (PermissionUtils.isGrantedDrawOverlays()) {
                    AppLimitService.start(getContext());
                }
            } else {
                AppLimitService.start(getContext());
            }
        }

    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.llMobile ||
                id == R.id.tvMobile) {
            clickMobile();
        } else if (id == R.id.llWifi ||
                id == R.id.tvWifi) {
            clickWifi();
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        AppNetUsageInfo usageInfo = mUsageAppAdapter.getItem(position);
        AppUsageDetailActivity.start(getContext(), usageInfo.pckName);
    }

    public void updateData() {
        NetUsageUtil.getAllInToday(new IRetCallback<NetUsageInfoList>() {
            @Override
            public void onResult(NetUsageInfoList result) {
                String mobile = ConvertUtils.byte2FitMemorySize(result.getMobileTotalBytes(), 1);
                String wifi = ConvertUtils.byte2FitMemorySize(result.getWifiTotalBytes(), 1);
                mDataBinding.tvMobileData.setText(mobile);
                mDataBinding.tvWifiData.setText(wifi);

                long total = result.getMobileTotalBytes() + result.getWifiTotalBytes();
                int mobilePercent = 0;
                int wifiPercent = 0;
                if (total > 0) {
                    mobilePercent = (int) (result.getMobileTotalBytes() * 1f / total * 100);
                    wifiPercent = 100 - mobilePercent;
                }
                mDataBinding.tvMobilePercent.setText(mobilePercent + "%");
                mDataBinding.tvWifiPercent.setText(wifiPercent + "%");
            }
        });

        NetUsageUtil.getAllAppUsageInfoInToday(new IRetCallback<List<AppNetUsageInfo>>() {
            @Override
            public void onResult(List<AppNetUsageInfo> result) {
                mUsageAppAdapter.setNewInstance(result);
                mUsageAppAdapter.setShowMobie(mUsageAppAdapter.isShowMobie());
            }
        });
    }

    private void clickMobile() {
        mUsageAppAdapter.setShowMobie(true);
    }

    private void clickWifi() {
        mUsageAppAdapter.setShowMobie(false);
    }
}
